package com.bookmanger.controller;

import cn.hutool.core.bean.BeanUtil;
import com.bookmanger.common.result.Result;
import com.bookmanger.entity.Book;
import com.bookmanger.entity.dto.BookSearchDTO;
import com.bookmanger.entity.dto.ModifyBook;
import com.bookmanger.entity.request.BookSearchRequest;
import com.bookmanger.entity.select.BookListDTO;
import com.bookmanger.entity.vo.BookCategoryCountVO;
import com.bookmanger.entity.vo.BookListVo;
import com.bookmanger.entity.vo.ListForm;
import com.bookmanger.service.BookSearchService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @folder 图书查询
 * @author wei kunJie
 * @date 2024/11/2 14:39:35
 */
@RestController
@RequestMapping("/api/bookSearch")
@Slf4j
public class BookSearchController {

    private BookSearchService bookSearchService;

    public BookSearchController(BookSearchService bookSearchService) {
        this.bookSearchService = bookSearchService;
    }

    /**
     * 搜索图书
     * 按书名/作者/ISBN/分类搜索
     * @param request
     * @return
     */
    @GetMapping("/search")
    public Result<List<BookSearchDTO>> search(BookSearchRequest request){
        return bookSearchService.search(BeanUtil.copyProperties(request, Book.class));
    }

    /**
     * 图书详细
     * 图书卡片，展示图书ISBN、名字、作者、分类、封面、简介、库存、出版社、出版年、页数、定价、装帧
     * @param bookId
     * @return
     */
    @GetMapping("/detail")
    public Result<BookSearchDTO> detail(Long bookId){
        return bookSearchService.detail(bookId);
    }

    /**
     * 添加图书
     * @param bookSearchDTO
     * @return
     */
    @PostMapping("/addBook")
    public Result addBook(@RequestBody BookSearchDTO bookSearchDTO){
        log.info(bookSearchDTO.toString());
        return bookSearchService.addBook(bookSearchDTO);
    };

    /**
     * 修改图书
     * ISBN、名字、作者、分类、封面、简介、库存、出版社、出版年、页数、定价、装帧
     * @param bookSearch
     * @return
     */
    @PostMapping("/updateBook")
    public Result updateBook(@RequestBody ModifyBook bookSearch){
        return bookSearchService.updateBook(bookSearch);
    }

    /**
     * 上架图书
     * 图书id
     * @param id
     * @return
     */
    @GetMapping("/GroundBook")
    public Result groundBook(@RequestParam Long id){
        return bookSearchService.groundBook(id);
    };

    /**
     * 下架图书
     * 图书id
     * @param id
     * @return
     */
    @GetMapping("/unGroundBook")
    public Result unGroundBook(@RequestParam Long id){
        return bookSearchService.unGroundBook(id);
    };

    /**
     * 删除图书
     * 图书id
     * @param id
     * @return
     */
    @GetMapping("/deleteBook")
    public Result deleteBook(@RequestParam Long id){
        return bookSearchService.deleteBook(id);
    };

    /**
     * 获取图书列表
     * @param bookListDTO
     * @return
     */
    @PostMapping("/getBookList")
    public Result<ListForm<BookListVo>> getBookList(@RequestBody BookListDTO bookListDTO){
        return bookSearchService.getBookList(bookListDTO);
    }

    /**
     * 获取书籍总量
     * @return
     */
    @GetMapping("/getBookCount")
    public Result<Integer> getBookCount(){
        return bookSearchService.getBookCount();
    }

    /**
     * 获取书籍分类对应书籍的数量
     * @return
     */
    @GetMapping("/getBookCategoryCount")
    public Result<List<BookCategoryCountVO>> getBookCategoryCount(){
        return bookSearchService.getBookCategoryCount();
    }
}
